home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / processes / mp threaded sort / sortpicts.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  5.1 KB  |  219 lines

  1. /*
  2.     File:        SortPicts.h
  3.  
  4.     Contains:    
  5.  
  6.     Written by:     
  7.  
  8.     Copyright:    Copyright © 1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/27/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. #ifndef __SortPicts__
  25. #define __SortPicts__
  26.  
  27. class    SortPicts;
  28.  
  29. #include "Window.h"
  30. #include "Sprocket.h"
  31. #include "Multiprocessing.h"
  32.  
  33. #include <Threads.h>
  34. #include <QDOffscreen.h>
  35. #include <Sound.h>
  36. //#include <LowMem.h>
  37.  
  38. #define kUpdateInterval 5;
  39.  
  40. /*
  41.  *    QuickSort #define
  42.  */
  43. #define    kPivotCutoff    25
  44.  
  45. typedef    void    (*SortFuncPtr)( SortPicts *sortPicts);
  46. typedef    void    (*UpdateFuncPtr)( SortPicts *sortPicts);
  47.  
  48. typedef    unsigned char    SortPixel;
  49. typedef    SortPixel        *SortPixelPtr;
  50. typedef    SortPixelPtr    *SortPixelHandle;
  51.  
  52. typedef    long            SortData;
  53. typedef    SortData        *SortDataPtr;
  54. typedef SortDataPtr        *SortDataHandle;
  55.  
  56. #define    kYieldTime                    4
  57. #define    kWindPictRectVoffset        0
  58.  
  59. /* The names of windows to open at startup time */
  60. #define    kDefaultWindowResource        100
  61.  
  62. #include "Window.h"
  63.  
  64. class    SortPicts : public TWindow {
  65. public:
  66.     
  67.     
  68.     GWorldPtr            sortGWorld;
  69.     PixMapHandle        sortPixmap;
  70.     SortPixelPtr        sortPixels;
  71.     SortDataPtr            sortData;
  72.     SortDataHandle        sortHandle;
  73. static long                gNumWindowsCreated;
  74. static short            gPrefsRef;
  75.  
  76.     long                N;
  77.  
  78.     long                pictWidth;
  79.     long                pictHeight;
  80.     long                pictRowBytes;
  81.     
  82.     Rect                updateRect;
  83.     Rect                sortRect;
  84.     Rect                copyBitsRect;
  85.     Rect                windPictRect;
  86.  
  87.     long                low, high;
  88.  
  89.     SortFuncPtr            sortFunc;
  90.     UpdateFuncPtr        updateFunc;
  91.  
  92.     PicHandle            sortPict;
  93.     WindowPtr            me;
  94.  
  95.     long                randomSeed;
  96.  
  97.  
  98.     long                startTime;
  99.     long                newUnitCount;
  100.     long                unitCount;
  101.  
  102.     ThreadID            threadInfo;
  103.     
  104.     Boolean                coplandTask;
  105. //    TaskName            myTaskName;
  106.     MPTaskID            myTaskID;
  107.     MPCriticalRegionID    sortBlitterBusy;
  108.     unsigned long        updateTime;
  109.     
  110.     long                horiz;
  111.     long                vert;
  112.     long                lastHorizLeft;
  113.     long                lastHorizRight;
  114.     long                pixelVert;
  115.     
  116. public:
  117.                          SortPicts();
  118.                          SortPicts( ConstStr255Param name);
  119.     virtual             ~SortPicts();
  120.  
  121.     virtual    void        Idle(EventRecord *anEvent);
  122.     virtual WindowPtr    MakeNewWindow(WindowPtr behindWindow);
  123.     virtual    void        AdjustCursor(EventRecord * anEvent);
  124.     virtual void        Activate(Boolean activating);
  125.     virtual void        Draw(void);
  126.     virtual    void        Drag(Point startPoint);
  127.  
  128. //    virtual void        Click(EventRecord * anEvent);
  129.     virtual    void        KeyDown(EventRecord * anEvent);
  130.  
  131.     virtual void        AdjustForNewWindowSize(Rect * oldRect,Rect * newRect);
  132.  
  133.     virtual    Boolean        Close(void);
  134.     
  135.     virtual    OSErr        HandleDrag(DragTrackingMessage dragMessage,DragReference theDrag);
  136.     virtual    OSErr        HandleDrop(DragReference theDrag);
  137.  
  138.     Boolean                PrepareGWorld( ConstStr255Param pictName);
  139.     
  140.     Boolean                LoadSortPicture( ConstStr255Param pictName);
  141.     void                ReleaseSortPicture( void);
  142.     Boolean                MakeSortGWorld( void);
  143.     Boolean                AllocSortData( void);
  144.     void                CopyFromSortData( void );
  145.     void                ReleaseSortData( void);
  146.     void                MakeSortData( void);
  147.     void                UseSortData( void);
  148.     void                UnuseSortData( void);
  149.     void                InitializeSortWindowRect( ConstStr255Param pictName);
  150.     void                OpenDefaultSortWindows( void);
  151.     void                SelectRandomPicture( void);
  152.     
  153.     Str255                sortPictsName;
  154.     long                Random( long num);
  155.     long                RandomPixel( void);
  156.     void                SetSortItem( long index, long data);
  157.     void                ExchangeSortItem( long one, long theOther);
  158.     void                Scramble( void);
  159.  
  160.     void                UseUpdateFunc( UpdateFuncPtr updateFunc);
  161.     void                Update( void);
  162.     void                UpdateScramble( void);
  163.     void                UpdateSortPict( void);
  164.     
  165.     void                Entry( void);
  166.     void                MakeThreaded( void);
  167.     void                Yield( void);
  168.     void                CalculateNewUnitCount( void);
  169.     
  170.     void                UseSortFunc( SortFuncPtr sortFunc);
  171.     void                Sort( void);
  172.  
  173.     void                QSort( void);
  174.     void                ChooseMedian( long a, long b, long c);
  175.     void                QuickSortEngine( long left, long right);
  176.  
  177.     void                HeapSort( void);
  178.     void                BuildHeap( void);
  179.     void                Heapify( long size, long index);
  180.     
  181.     void                ShellSort( void);
  182. };
  183.  
  184. void    UpdateScramble( SortPicts *sortPicts);
  185. void    UpdateSortPict( SortPicts *sortPicts);
  186. void    QSort( SortPicts *sortPicts);
  187. void    HeapSort( SortPicts *sortPicts);
  188. void    ShellSort( SortPicts *sortPicts);
  189.  
  190. pascal    voidPtr    SortPictsThreadEntry( void *there);
  191.  
  192.  
  193. //    menus defined by SortPicts
  194.  
  195. #ifndef        _STANDARDMENUS_
  196. #include    "StandardMenus.h"
  197. #endif
  198.  
  199. #define    mSprocketFileMenu                256
  200. #define        iNewSortingPict                1
  201. #define        iCloseWindow                2
  202. #define        iQuitSortPicts                4
  203.  
  204. #define    mSortAlgorithm                    257
  205. #define        iQuickSort                    1
  206. #define        iShellSort                    2
  207. #define        iHeapSort                    3
  208. #define        iRandomSort                    5
  209. #define        iUseCoplandTasks            7
  210.  
  211. #define    mPictureMenu                    258
  212.  
  213. #define    kAboutBoxFor68K                    256
  214. #define    kAboutBoxForPowerPC                257
  215. #define        iCreditsButton                    2
  216.  
  217. #define    kCreditsBox                        258
  218.  
  219. #endif